package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.wx.WxBaseRespVo;
import com.cskaoyan.bean.wxbean.WxGoodsDetailBean.CommentWx;
import com.cskaoyan.bean.wxbean.WxGoodsDetailBean.GoodsDetailWx;
import com.cskaoyan.bean.wxbean.WxGoodsDetailBean.SpecificationWx;
import com.cskaoyan.bean.wxbean.WxGoodsDetailBean.WxGroupon;
import com.cskaoyan.bean.wxbean.WxHomeBean.GoodsListBean;
import com.cskaoyan.mapper.FootprintMapper;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.service.wx.CategoryService;
import com.cskaoyan.service.wx.WxFootprintService;
import com.cskaoyan.service.wx.WxGoodsDetailService;
import com.cskaoyan.utils.IdUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("wx/goods/")
public class WxGoodsController {
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    WxGoodsDetailService wxGoodsDetailService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    WxFootprintService footprintService;


    @RequestMapping("count")
    public WxBaseRespVo goodscount() {
        GoodsExample goodsExample = new GoodsExample();
        long count = goodsMapper.countByExample(goodsExample);
        Map<String, String> data = new HashMap<>();
        data.put("goodsCount",Long.toString(count));
        return WxBaseRespVo.ok(data);
    }
    @RequestMapping("detail")
    public WxBaseRespVo goodsDetail(Integer id){
        Integer userId = IdUtils.getId();
        GoodsDetailWx data = getGoodsDetailById(id, userId);
        footprintService.insertFootprint(id,userId);
        return WxBaseRespVo.ok(data);
    }

    private GoodsDetailWx getGoodsDetailById(Integer goodsId, Integer userId) {
        GoodsDetailWx goodsDetailWx = new GoodsDetailWx();
        Goods goods = wxGoodsDetailService.queryOneById(goodsId);
        List<Attribute> attribute = wxGoodsDetailService.getAttributeByGoodsId(goodsId);
        Brand brand = null;
        if (goods.getBrandId() != 0 && goods.getBrandId() != null){
            brand = wxGoodsDetailService.getBrandById(goods.getBrandId());
        }
        HashMap<String, Object> comment = new HashMap<>();
        List<CommentWx> comments = wxGoodsDetailService.getCommentsByGoodsId(goodsId);
        int count = wxGoodsDetailService.getCommentCountByGoodsId(goodsId);
        comment.put("data",comments);
        comment.put("count", count);
        List<WxGroupon> groupon = new ArrayList<>();
        Goods info = wxGoodsDetailService.queryOneById(goodsId);
        List<Issue> issue = wxGoodsDetailService.getAllIssue();
        List<Product> productList = wxGoodsDetailService.queryByGoodsId(goodsId);
        String shareImage = "";
        List<SpecificationWx> specificationList = wxGoodsDetailService.querySpecificationWxByGoodsId(goodsId);
        int isCollected = 0;
        if (userId != null){
            isCollected = wxGoodsDetailService.verifyCollectByUserIdAndGoodsId(goodsId, userId);
        }
        goodsDetailWx.setAttribute(attribute);
        goodsDetailWx.setBrand(brand);
        goodsDetailWx.setComment(comment);
        goodsDetailWx.setGroupon(groupon);
        goodsDetailWx.setInfo(info);
        goodsDetailWx.setIssue(issue);
        goodsDetailWx.setProductList(productList);
        goodsDetailWx.setShareImage(shareImage);
        goodsDetailWx.setSpecificationList(specificationList);
        goodsDetailWx.setUserHasCollect(isCollected);
        return goodsDetailWx;
    }

    @RequestMapping("category")
    public WxBaseRespVo goodsCategory(Integer id){
        HashMap<String, Object> currentSubCategory = getSubCategoryById(id);
        WxBaseRespVo baseRespVO = WxBaseRespVo.ok(currentSubCategory);
        return baseRespVO;
    }

    private HashMap<String, Object> getSubCategoryById(Integer currentSubCategoryId) {
        HashMap<String, Object> hashMap = new HashMap<>();
        Integer parentCategoryId = categoryService.queryPidById(currentSubCategoryId);

        List<CategoryId> brotherCategory = null;
        CategoryId currentCategory = null;
        CategoryId parentCategory = null;
        if (parentCategoryId != 0){
            brotherCategory = categoryService.queryAllCategoriesL2(parentCategoryId);
            currentCategory = categoryService.getCategoryById(currentSubCategoryId);
            parentCategory = categoryService.getCategoryById(parentCategoryId);
        }else {
            brotherCategory = categoryService.queryAllCategoriesL2(currentSubCategoryId);
            CategoryId firstCategory = brotherCategory.get(0);
            currentCategory = categoryService.getCategoryById(firstCategory.getId());
            parentCategory = categoryService.getCategoryById(currentSubCategoryId);
        }

        hashMap.put("brotherCategory", brotherCategory);
        hashMap.put("currentCategory",currentCategory);
        hashMap.put("parentCategory", parentCategory);
        return hashMap;
    }

    @RequestMapping("list")
    public WxBaseRespVo goodsList( String keyword,String order,String sort,Integer categoryId,Integer page,Integer size){
        Map<String, Object> data = new HashMap<>();
        List<GoodsListBean> goodsList;
        List<CategoryId> filterCategoryList;
        PageHelper.startPage(page, size);
        if(keyword == null){
            goodsList = categoryService.queryGoodsListById(categoryId);
            long total1 = new PageInfo(goodsList).getTotal();
            data.put("goodsList",goodsList);
            data.put("count", total1);
            return WxBaseRespVo.ok(data);
        }else {
            filterCategoryList = categoryService.queryCategoryListByKeyWord(order, sort, keyword);
            goodsList = categoryService.queryGoodsListByKeyWord(order, sort, keyword);
            long total1 = new PageInfo(goodsList).getTotal();
            data.put("goodsList", goodsList);
            data.put("count", total1);
            data.put("filterCategoryList", filterCategoryList);
            return WxBaseRespVo.ok(data);
        }
    }

    @RequestMapping("related")
    public WxBaseRespVo goodsRelated(Integer id){
        Map<String, Object>  data = categoryService.queryWxRelatedList(id);
        return WxBaseRespVo.ok(data);
    }

}
